<template>

  <div class="app-container">

    <!-- 过滤搜索框 START-->
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="昵称" v-model="filter.columns.nickname">
      </el-input>
      <el-select @change="handleFilter" clearable style="width: 100px" class="filter-item" v-model="filter.columns.sex" placeholder="性别">
        <el-option label="女" value="0"></el-option>
        <el-option label="男" value="1"></el-option>
      </el-select>
      <el-select @change="handleFilter" clearable style="width: 120px" class="filter-item" v-model="filter.columns.type" placeholder="用户类型">
        <el-option key="后台创建" label="后台创建" value="0"> </el-option>
        <el-option key="注册加入" label="注册加入" value="1"> </el-option>
      </el-select>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">添加</el-button>
      <el-button class="filter-item" type="primary" :loading="downloadLoading" v-waves icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>
    <!-- 过滤搜索框 END -->

    <!-- 表格呈现 START -->
    <el-table ref="demoTable" @selection-change="handleSelectionChange" element-loading-text="加载中" v-loading="listLoading" fit highlight-current-row border :data="list">
      <el-table-column prop="index" type="index"></el-table-column>
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="code" label="用户编号" width="120"></el-table-column>
      <el-table-column prop="avatar" align="center" label="头像" width="60">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <img :src="getRealImgPath(scope.row.avatar)" width="160" />
            <div slot="reference" class="name-wrapper">
              <img :src="getRealImgPath(scope.row.avatar)" width="30" />
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户类型" width="110">
        <template slot-scope="scope">
          <el-tag :type="scope.row.type | typeFilter">{{scope.row.type}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="sex" align="center" label="性别" width="70"></el-table-column>
      <el-table-column label="用户昵称" prop="nickname"></el-table-column>
      <el-table-column prop="phone" label="手机号" width="120"></el-table-column>
      <el-table-column align="center" label="账号状态" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isDelete | deleteFilter">{{scope.row.isDelete == false ? '启用':'禁用'}}</el-tag>
        </template>
      </el-table-column>
      <!-- 详情展开内容 -->
      <el-table-column type="expand" label="详情" width="80">
        <template slot-scope="props">

          <!-- 用户信息详情卡片 START -->
          <el-card>
            <span>用户信息</span>
            <el-form label-position="left" inline label-width="220">
              <el-form-item label="创建时间">
                <i class="el-icon-time"></i>
                <span style="margin-left: 10px">{{ props.row.createTime }}</span>
              </el-form-item>
              <el-form-item label="更新时间">
                <i class="el-icon-time"></i>
                <span style="margin-left: 10px">{{ props.row.updateTime }}</span>
              </el-form-item>
            </el-form>
          </el-card>
          <!-- 用户详情卡片 END -->

          <!-- 用户角色卡片 START -->
          <el-card v-if="props.row.roleList">
            <span>用户角色</span>
            <el-form label-position="left" inline label-width="220">
              <div v-for="role in props.row.roleList" :key="role.id">
                <el-form-item label="角色名">
                  <span>{{ role.role }}</span>
                </el-form-item>
                <el-form-item label="角色描述">
                  <span>{{ role.description }}</span>
                </el-form-item>
              </div>
            </el-form>
          </el-card>
          <!-- 用户角色卡片 END -->

          <!-- 用户账号卡片　START -->
          <el-card v-if="props.row.userAuthEntityList">
            <span>登录账号</span>
            <el-form label-position="left" inline label-width="220">
              <div v-for="userAUth in props.row.userAuthEntityList" :key="userAUth.id">
                <el-form-item label="登录账号">
                  <span>{{ userAUth.loginAccount }}</span>
                </el-form-item>
                <el-form-item label="账号类型">
                  <span>{{ userAUth.loginType }}</span>
                </el-form-item>
                <el-form-item label="登录IP">
                  <span>{{ userAUth.lastLoginIp }}</span>
                </el-form-item>
                <el-form-item label="登录设备">
                  <span>{{ userAUth.lastLoginTime }}</span>
                </el-form-item>
                <el-form-item label="账号状态">
                  <el-tag :type="userAUth.status | statusFilter">{{userAUth.status}}</el-tag>
                </el-form-item>
              </div>
            </el-form>
          </el-card>
          <!-- 用户账号卡片 END -->

          <!-- 学校信息 卡片 START -->
          <el-card v-if="props.row.userInfoSchoolEntity">
            <span>学生信息</span>

            <el-form label-position="left" inline label-width="220">
              <el-form-item label="真实姓名">
                <span>{{ props.row.userInfoSchoolEntity.realName }}</span>
              </el-form-item>
              <el-form-item label="学院">
                <span>{{ props.row.userInfoSchoolEntity.academy }}</span>
              </el-form-item>
              <el-form-item label="学号">
                <span>{{ props.row.userInfoSchoolEntity.academy }}</span>
              </el-form-item>
              <el-form-item label="学校">
                <span>{{ props.row.userInfoSchoolEntity.school }}</span>
              </el-form-item>
              <el-form-item label="专业">
                <span>{{ props.row.userInfoSchoolEntity.special }}</span>
              </el-form-item>
              <el-form-item label="入学时间">
                <span>{{ props.row.userInfoSchoolEntity.entryTime }}</span>
              </el-form-item>
              <el-form-item label="审核状态">
                <el-tag :type="props.row.userInfoSchoolEntity.status | schoolStatusFilter">{{props.row.userInfoSchoolEntity.status}}</el-tag>
              </el-form-item>
              <el-form-item label="认证照片">
                <el-popover trigger="hover" placement="left">
                  <img :src="getRealImgPath(props.row.userInfoSchoolEntity.cardPhoto)" width="500" />
                  <div slot="reference" class="name-wrapper">
                    <img :src="getRealImgPath(props.row.userInfoSchoolEntity.cardPhoto)" width="120" />
                  </div>
                </el-popover>
              </el-form-item>
            </el-form>
          </el-card>
          <!-- 学校信息 卡片 END -->

        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="230">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-show="!scope.row.isDelete" size="mini" type="danger" @click="handleUpdateDelete(scope.row,true)">禁用用户</el-button>
          <el-button v-show="scope.row.isDelete" size="mini" type="warning" @click="handleUpdateDelete(scope.row,false)">启用用户</el-button>
        </template>
      </el-table-column>

    </el-table>
    <!-- 表格呈现 END -->

    <!-- 分页导航栏 START -->
    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10, 25, 50, 100]" :pager-count="11" :total="total" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 分页导航栏 END -->

    <!-- 编辑信息弹出框 START -->
    <el-dialog :title="dialogTitle[dialogStatus]" :visible.sync="dialogVisible">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="100px">
        <el-form-item label="昵称" prop="nickname">
          <el-input max="11" placeholder="请输入用户昵称" v-model.trim="temp.nickname">
          </el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="temp.sex" placeholder="请选择性别">
            <el-option key="男" label="男" value="男"> </el-option>
            <el-option key="女" label="女" value="女"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户类型" prop="type">
          <el-select v-model="temp.type" placeholder="请选择用户类型">
            <el-option key="后台创建" label="后台创建" value="后台创建"> </el-option>
            <el-option key="注册加入" label="注册加入" value="注册加入"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户状态" prop="isDelete">
          <el-select v-model="temp.isDelete" placeholder="请选择状态">
            <el-option key="启用" label="启用" :value="false"> </el-option>
            <el-option key="禁用" label="禁用" :value="true"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input max="11" placeholder="请输入手机号" v-model.number="temp.phone">
          </el-input>
        </el-form-item>
        <el-form-item label="编号" prop="code">
          <el-input max="11" placeholder="请输入编号" v-model="temp.code">
          </el-input>
        </el-form-item>
        <el-form-item label="用户头像" prop="avatar">
          <pan-thumb :image="getRealImgPath(temp.avatar)"></pan-thumb>
          <el-button type="primary" icon="upload" @click="imagecropperShow=true">上传头像</el-button>
          <image-cropper :width="300" :height="300" :url="uploadUrl" @close='imagecropperShow = false' @crop-upload-success="cropHeadSuccess" :key="imagecropperKey" v-show="imagecropperShow"></image-cropper>
        </el-form-item>

        <hr/>
        <div v-for="(userAuth,index) in temp.userAuthEntityList" :key="index">
          <span>登录方式 {{ index + 1 }}</span>
          <br/>
          <el-form-item label="账号" :rules="{ required: true, message: '登录账号不能为空', trigger: 'blur' }" :prop="`userAuthEntityList[${index}].loginAccount`">
            <el-input placeholder="请输入帐号" v-model="userAuth.loginAccount"> </el-input>
          </el-form-item>
          <el-form-item v-if="dialogStatus=='create'" label="密码" :rules="[{ required: true, message: '密码不能为空', trigger: 'blur' }, { min: 6, max: 25, message: '长度在 6 到 25 个字符', trigger: 'blur' }]" :prop="`userAuthEntityList[${index}].loginPassword`">
            <el-input type="password" placeholder="请输入密码" v-model="userAuth.loginPassword"> </el-input>
          </el-form-item>
          <el-form-item v-if="dialogStatus=='update'" label="密码" :prop="`userAuthEntityList[${index}].loginPassword`" :rules="{ min: 6, max: 25, message: '长度在 6 到 25 个字符', trigger: 'blur' }">
            <el-input type="password" placeholder="如需修改密码在此处填写" v-model="userAuth.loginPassword"> </el-input>
          </el-form-item>
          <el-form-item label="登录方式" :rules="{ required: true, message: '登录方式不能为空', trigger: 'blur' }" :prop="`userAuthEntityList[${index}].loginType`">
            <el-select v-model="userAuth.loginType" placeholder="请选择登录方式">
              <el-option key="USERNAME" label="用户名" value="USERNAME"> </el-option>
              <el-option key="PHONE" label="手机号" value="PHONE"> </el-option>
              <el-option key="WEXIN" label="微信" value="WEXIN"> </el-option>
              <el-option key="QQ" label="QQ" value="QQ"> </el-option>
              <el-option key="EMAIL" label="邮箱" value="EMAIL"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="账号状态" :rules="{ required: true, message: '不能为空', trigger: 'blur' }" :prop="`userAuthEntityList[${index}].isDelete`">
            <el-select v-model="userAuth.isDelete" placeholder="请选择状态">
              <el-option key="启用" label="启用" :value="false"> </el-option>
              <el-option key="禁用" label="禁用" :value="true"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="deleteLoginAuth(index)">取消登录方式</el-button>
          </el-form-item>
        </div>
        <el-form-item>
          <el-button @click="addLoginAuth">新增登录方式</el-button>
        </el-form-item>

        <hr/>
        <div v-for="(role,index) in temp.roleList" :key="role.id">
          <span>用户角色 {{ index + 1 }}</span>
          <el-form-item label="角色类型">
            <el-select v-model="role.id" placeholder="请选择角色">
              <el-option v-for="item in roleOptions" :key="item.id" :label="item.role + '  (' + item.description + ')'" :value="item.id">
              </el-option>
            </el-select>
            <el-button @click="deleteRole(index)">删除角色</el-button>
          </el-form-item>
        </div>

        <el-form-item>
          <el-button @click="addRole">新增角色</el-button>
        </el-form-item>

        <div v-if="ifHasRoleStudent">
          <hr/>
          <span>学生信息</span>
          <el-form-item label="学号" prop="userInfoSchoolEntity.code">
            <el-input max="11" placeholder="请输入学号" v-model="temp.userInfoSchoolEntity.code">
            </el-input>
          </el-form-item>
          <el-form-item label="学校" prop="userInfoSchoolEntity.school">
            <el-input max="30" placeholder="请输入学校" v-model="temp.userInfoSchoolEntity.school">
            </el-input>
          </el-form-item>
          <el-form-item label="学院" prop="userInfoSchoolEntity.academy">
            <el-input max="30" placeholder="请输入学院" v-model="temp.userInfoSchoolEntity.academy">
            </el-input>
          </el-form-item>
          <el-form-item label="专业" prop="userInfoSchoolEntity.special">
            <el-input max="30" placeholder="请输入专业" v-model="temp.userInfoSchoolEntity.special">
            </el-input>
          </el-form-item>
          <el-form-item label="入学日期" prop="userInfoSchoolEntity.entryTime">
            <el-date-picker format="yyyy-MM-dd" value-format="yyyy-MM-dd" v-model="temp.userInfoSchoolEntity.entryTime" type="date" placeholder="请选择入学日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="真实姓名" prop="userInfoSchoolEntity.realName">
            <el-input max="30" placeholder="请输入学生真实姓名" v-model="temp.userInfoSchoolEntity.realName">
            </el-input>
          </el-form-item>
          <el-form-item label="认证照片" prop="userInfoSchoolEntity.cardPhoto">
            <img :src="getRealImgPath(temp.userInfoSchoolEntity.cardPhoto)" />
            <el-button type="primary" icon="upload" @click="imagecropperShow=true">上传照片</el-button>
            <image-cropper :width="300" :height="300" :url="uploadUrl" @close='imagecropperShow = false' @crop-upload-success="cropCardSuccess" :key="imagecropperKey" v-show="imagecropperShow"></image-cropper>
          </el-form-item>
          <el-form-item label="审核状态" prop="userInfoSchoolEntity.status" :rules="{ required: true, message: '审核状态必填', trigger: 'blur' }">
            <el-select v-model="temp.userInfoSchoolEntity.status" placeholder="请选择审核状态">
              <el-option key="0" label="待审核" value="待审核"> </el-option>
              <el-option key="1" label="审核通过" value="审核通过"> </el-option>
              <el-option key="2" label="审核不通过" value="审核不通过"> </el-option>
            </el-select>
          </el-form-item>
        </div>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button :disabled="dialogLoading" v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button :disabled="dialogLoading" v-else type="primary" @click="updateData">修改</el-button>
      </div>
    </el-dialog>
    <!-- 编辑信息弹出框 END -->

  </div>
</template>

<script>
import apiUser from '@/api/admin/user.js'
import apiRole from '@/api/admin/role.js'
import waves from '@/directive/waves' // 水波纹特效 指令
import { getToken } from '@/utils/auth'
// 上传头像,预览组件
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'

export default {
  name: 'account',
  components: { ImageCropper, PanThumb },
  directives: { waves },
  data() {
    return {
      imagecropperShow: false,
      imagecropperKey: 0,
      importFileUrl: process.env.BASE_API + '/upload',
      upLoadData: {
        path: 'test'
      },
      downloadLoading: false,
      total: 0,
      // 临时对象,用于编辑修改操作
      temp: {},
      // 弹出框表单状态
      dialogStatus: undefined,
      dialogTitle: {
        create: '创建用户',
        update: '修改用户'
      },
      dialogVisible: false,
      dialogLoading: false,
      roleOptions: [],
      // 编辑框校验规则
      rules: {
        nickname: [
          { required: true, message: '昵称不能为空', trigger: 'blur' },
          { min: 1, max: 25, message: '长度在 1 到 25 个字符', trigger: 'blur' }
        ],
        code: [{ required: true, message: '编号不能为空', trigger: 'blur' }],
        sex: [{ required: true, message: '性别必填', trigger: 'change' }],
        phone: [
          { pattern: /^1[34578]\d{9}$/, message: '目前只支持中国大陆的手机号码', trigger: 'blur' },
          { required: true, message: '手机号必填', trigger: 'change' }
        ]
      },
      list: [],
      listLoading: true,
      // 查询条件
      listQuery: {
        page: 1,
        size: 10,
        sorts: [],
        columns: {}
      },
      // 过滤框条件
      filter: {
        columns: {
          name: undefined,
          sex: undefined,
          type: undefined
        }
      },
      // 多选框选中对象
      multipleSelection: []
    }
  },
  watch: {
    listQuery: {
      handler(val) {
        this.getData(val)
      },
      deep: true
    },
    dialogVisible: {
      handler(val) {
        // 出现弹框的时候手动加载角色列表
        if (val) {
          if (this.roleOptions.length === 0) {
            apiRole.search({ page: 1, size: 100 }).then(res => {
              if (res.status === 200) {
                this.roleOptions = res.data.list
              }
            })
          }
        }
      }
    }
  },
  filters: {
    deleteFilter(isDelete) {
      const deleteMap = {
        'true': 'danger',
        'false': 'success'
      }
      return deleteMap[isDelete]
    },
    schoolStatusFilter(status) {
      const statusMap = {
        '待审核': 'info',
        '审核通过': 'success',
        '审核不通过': 'danger'
      }
      return statusMap[status]
    },
    statusFilter(status) {
      const statusMap = {
        '启用': 'success',
        '禁用': 'danger'
      }
      return statusMap[status]
    },
    typeFilter(type) {
      const typeMap = {
        '后台创建': 'info',
        '注册加入': 'success'
      }
      return typeMap[type]
    }
  },
  methods: {
    // v-el-drag-dialog onDrag callback function
    handleDrag() {
      this.$refs.select.blur()
    },
    // 动态添加一个角色
    addRole() {
      this.temp.roleList.push({})
    },
    deleteRole(index) {
      this.temp.roleList.splice(index, 1)
    },
    // 动态添加一个登录账号
    addLoginAuth() {
      this.temp.userAuthEntityList.push({
        id: 0,
        isDelete: false,
        loginAccount: '',
        loginPassword: '',
        loginType: 'USERNAME',
        status: '启用'
      })
    },
    // 动态移除一个登录账号
    deleteLoginAuth(index) {
      this.temp.userAuthEntityList.splice(index, 1)
    },
    // 判断图片路径是相对路径还是绝对路径
    getRealImgPath(avatarPath) {
      if (avatarPath && avatarPath.indexOf('http') < 0) {
        return this.imgBaseUrl + avatarPath
      } else {
        return avatarPath
      }
    },
    // 上传成功后的回调
    uploadSuccess(response, file, fileList) {
      this.$notify({ title: '成功', message: '上传成功', type: 'success', duration: 2000 })
    },
    // 上传错误
    uploadError(response, file, fileList) {
      this.$notify({ title: '失败', message: '上传失败', type: 'error', duration: 2000 })
    },
    // 上传前对文件的大小的判断
    beforeAvatarUpload(file) {
      const extension = file.name.split('.')[1] === 'xls'
      const extension2 = file.name.split('.')[1] === 'xlsx'
      const extension3 = file.name.split('.')[1] === 'doc'
      const extension4 = file.name.split('.')[1] === 'docx'
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!extension && !extension2 && !extension3 && !extension4) {
        console.log('上传模板只能是 xls、xlsx、doc、docx 格式!')
      }
      if (!isLt2M) {
        console.log('上传模板大小不能超过 10MB!')
      }
      return extension || extension2 || extension3 || extension4 && isLt2M
    },
    filterSex(value, row) {
      return row.sex === value
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogVisible = true
      this.dialogLoading = false
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        sex: undefined,
        phone: undefined,
        type: undefined,
        isDelete: false,
        code: undefined,
        nickname: undefined,
        avatar: 'https://image.flaticon.com/icons/png/128/145/145859.png',
        userAuthEntityList: [{
          id: 0,
          isDelete: false,
          loginAccount: '',
          loginPassword: '',
          loginType: 'USERNAME',
          status: '启用'
        }],
        roleList: []
      }
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dialogLoading = true
          apiUser.create(this.temp).then(res => {
            this.dialogLoading = false
            // 状态为成功
            if (res.status === 200) {
              this.dialogVisible = false
              // 数据直接插入表格顶部
              this.list.unshift(this.temp)
              this.$notify({ title: '成功', message: '创建成功', type: 'success', duration: 2000 })
            }
          })
        }
      })
    },
    handleUpdate(row) {
      // this.temp = Object.assign({}, row) // copy obj
      this.temp = JSON.parse(JSON.stringify(row))
      if (!this.temp.userInfoSchoolEntity) {
        this.temp.userInfoSchoolEntity = {}
      }
      this.dialogStatus = 'update'
      this.dialogLoading = false
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      // 复制对象
      const tempData = Object.assign({}, this.temp)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dialogLoading = true
          apiUser.update(tempData).then(res => {
            this.dialogLoading = false
            if (res.status === 200) {
              this.dialogVisible = false
              this.$notify({ title: '成功', message: '更新成功', type: 'success', duration: 2000 })
              // 将修改后的对象替换进列表中
              this.getData()
            }
          })
        }
      })
    },
    handleUpdateDelete(row, flag) {
      this.temp = JSON.parse(JSON.stringify(row))
      this.temp.isDelete = flag
      apiUser.update(this.temp).then(res => {
        if (res.status === 200) {
          row.isDelete = flag
          this.$notify({ title: '成功', message: '操作成功', type: 'success', duration: 2000 })
        }
      })
    },
    handleDeleteMulti() {
      const rows = this.multipleSelection
      const params = Array(0)
      rows.forEach(row => {
        params.push(row.id)
      })

      if (params.length > 0) {
        apiUser.removeMulti(params).then(res => {
          if (res.status === 200) {
            this.$message({
              message: res.data,
              type: 'success'
            })
            rows.forEach(row => {
              const index = this.list.indexOf(row)
              this.list.splice(index, 1)
            })
          }
        })
      } else {
        this.$message({
          message: '请先勾选列表',
          type: 'warning'
        })
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log(this.multipleSelection)
    },
    handleSizeChange(size) {
      this.listQuery.size = size
    },
    handleCurrentChange(page) {
      this.listQuery.page = page
    },
    getData() {
      this.listLoading = true
      apiUser.search(this.listQuery).then(res => {
        this.listLoading = false
        if (res.status === 200) {
          this.list = res.data.list
          this.total = res.data.total
        }
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.listQuery.columns = JSON.parse(JSON.stringify(this.filter.columns))
      // 昵称字段模糊搜索
      if (this.listQuery.columns.nickname && this.listQuery.columns.nickname !== '') {
        this.listQuery.columns.nickname = '%' + this.listQuery.columns.nickname + '%'
      }
      // listQuery已经被watch 会自动刷新数据
      // this.getData()
    },
    // 头像上传成功回调
    cropHeadSuccess(resData) {
      this.imagecropperShow = false
      this.imagecropperKey = this.imagecropperKey + 1
      // 这里返回的是相对路径
      this.temp.avatar = resData.url
    },
    // 认证照片上传成功回调
    cropCardSuccess(resData) {
      this.imagecropperShow = false
      this.imagecropperKey = this.imagecropperKey + 1
      // 这里返回的是相对路径
      this.temp.userInfoSchoolEntity.cardPhoto = resData.url
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['code', 'type', 'nickname', 'createTime', 'phone']
        const filterVal = ['code', 'type', 'nickname', 'createTime', 'phone']
        const data = this.formatJson(filterVal, this.list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '用户表格'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        // 字段的格式处理
        return v[j]
      }))
    }
  },
  computed: {
    imgBaseUrl: function() {
      return process.env.BASE_API
    },
    uploadUrl: function() {
      return process.env.BASE_API + '/file/upload'
    },
    tokenHeader: function() {
      return { 'X-token': getToken() }
    },
    ifHasRoleStudent: function() {
      let hasStudent = false
      // 判断用户角色中是否包含学生角色
      if (this.temp && this.temp.roleList) {
        const that = this
        this.temp.roleList.forEach(function(role) {
          // id=9是学生的id
          if (role.id === 9) {
            hasStudent = true
            if (!that.temp.userInfoSchoolEntity) {
              that.temp.userInfoSchoolEntity = {}
            }
          }
        })
      }
      return hasStudent
    }
  },
  created: function() {
    this.getData(this.listQuery)
  },
  mounted: function() {
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.documentation-container {
  margin: 50px;
  .document-btn {
    float: left;
    margin-left: 50px;
    display: block;
    background: black;
    color: white;
    height: 60px;
    width: 200px;
    line-height: 60px;
    font-size: 20px;
    text-align: center;
  }
}
</style>
